{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "23230e70-af04-4064-94f5-cde80a86df62",
   "metadata": {},
   "source": [
    "目前来讲，在工业界最常见的4种query增强\n",
    "\n",
    "之前写过一篇公众号发过推文：\n",
    "https://mp.weixin.qq.com/s?__biz=MzkyOTU5NzY1Mw==&mid=2247487865&idx=1&sn=58cdbb0dfed1545ff1e55b2ffd63eceb&chksm=c20640c3f571c9d5e041e18d6a97f42ffac6f37fceee898b55214db55c5183c9cfd63e100f71&token=1705800919&lang=zh_CN#rd\n",
    "\n",
    "1. query rewrite  问题改写\n",
    "2. step-back prompt  问题回退，取到高级概念，然后再知道推理\n",
    "3. hyde  借助大模型的answer格式  -> from llama_index.core.indices.query.query_transform import HyDEQueryTransform\n",
    "4. sub quesiton  子问题\n",
    "- > from llama_index.core.query_engine import SubQuestionQueryEngine\n",
    "- > from llama_index.core.indices.query.query_transform.base import StepDecomposeQueryTransform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8a76a544-dcb8-4df7-a542-6b580b2b45c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "prompt_query_rewrite = \"\"\"你是一个AI助手，负责重新表述用户问题，以提高在RAG系统中的检索效果。根据原始问题，将其改写得更具体、详细，并更可能检索到相关信息。\n",
    "\n",
    "返回格式{{\"rewrite_query\":\"\"}}\n",
    "\n",
    "原始问题: {original_query}\n",
    "\n",
    "改写后的问题：\"\"\"\n",
    "\n",
    "\n",
    "prompt_step_back = \"\"\"你是一个AI助手，负责生成更广泛、更加一般化的问题，以提高在RAG系统中的背景信息检索效果。根据原始查询，生成一个更一般化的问题，以帮助检索相关的背景信息。\n",
    "\n",
    "原始问题: {original_query}\n",
    "\n",
    "一般化问题：\"\"\"\n",
    "\n",
    "\n",
    "prompt_sub_question = \"\"\"你是一个AI助手，负责将复杂问题分解为更简单的子问题，以便在RAG系统中更有效地处理。根据原始问题，将其拆分成2-4个更简单的子问题，这些子问题的回答合起来可以全面回应原始查询。\n",
    "\n",
    "原始问题: {original_query}\n",
    "\n",
    "返回格式{{\"sub_query\":[]}}\n",
    "\n",
    "例如：气候变化对环境的影响是什么？\n",
    "\n",
    "子问题：\n",
    "{{\"sub_query\":[\"气候变化对生物多样性的影响是什么？\",\"气候变化如何影响海洋？\",\"气候变化对农业的影响是什么？\"]}}\"\"\"\n",
    "\n",
    "hyde_prompt = \"\"\"将以下问题 '{query}' 生成一个假设性的文档，该文档直接回答这个问题。文档应详细且深入。文档的大小必须恰好为 {chunk_size} 字符。\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "680b2aa4-7836-4293-b4d6-32bc2707a49e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from zhipuai import ZhipuAI\n",
    "client = ZhipuAI(api_key=\".\") # 填写您自己的APIKey\n",
    "\n",
    "def llm_engine(messages, stop_sequences=None):\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"glm-4-plus\", \n",
    "        messages=messages,\n",
    "        stop=stop_sequences\n",
    "    )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "09e224f1-5c57-4aea-b2c8-af3249398906",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"rewrite_query\":\"2023年腾讯公司净利润是多少\"}\n"
     ]
    }
   ],
   "source": [
    "print(llm_engine([{'role':'user','content':prompt_query_rewrite.format_map({'original_query':'鹅厂今年净利润'})}]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "32d73eae-74b0-4bab-88a0-6978edf10334",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "如何掌握基本运动技能\n"
     ]
    }
   ],
   "source": [
    "print(llm_engine([{'role':'user','content':prompt_step_back.format_map({'original_query':'怎么学会走路'})}]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c8758675-75ef-4980-b901-f5978b779aff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```json\n",
      "{\n",
      "  \"sub_query\": [\n",
      "    \"姚明的妻子是谁？\",\n",
      "    \"姚明是否有妹妹？\"\n",
      "  ]\n",
      "}\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "print(llm_engine([{'role':'user','content':prompt_sub_question.format_map({'original_query':'姚明的妻子是谁，姚明有妹妹吗'})}]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "82387c49-7c9e-4ffe-8857-d1be09ea3782",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```json\n",
      "{\n",
      "  \"sub_query\": [\n",
      "    \"姚明的妻子是谁？\",\n",
      "    \"姚明妻子的家庭成员中是否有妹妹？\"\n",
      "  ]\n",
      "}\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "print(llm_engine([{'role':'user','content':prompt_sub_question.format_map({'original_query':'姚明的妻子是谁，她有妹妹吗'})}]))\n",
    "\n",
    "# 只能应对并列的拆解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "933c9418-226c-4b96-b8b8-cde1a3dd983f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**北京今日天气报告**\n",
      "\n",
      "日期：2023年10月5日\n",
      "\n",
      "地点：北京市\n",
      "\n",
      "今日北京的天气状况如下：\n",
      "\n",
      "**气温概况：**\n",
      "- **最高气温**：22摄氏度\n",
      "- **最低气温**：12摄氏度\n",
      "- **当前气温**：18摄氏度（上午10时）\n",
      "\n",
      "**天气现象：**\n",
      "- **上午**：晴朗，阳光明媚，适宜外出。\n",
      "- **下午**：局部多云，偶有微风，不影响户外活动。\n",
      "- **傍晚**：云量增加，气温逐渐下降。\n",
      "\n",
      "**空气质量：**\n",
      "- **AQI指数**：85（良好）\n",
      "- **PM2.5浓度**：35微克/立方米\n",
      "- **PM10浓度**：50微克/立方米\n",
      "\n",
      "**其他气象信息：**\n",
      "- **相对湿度**：45%\n",
      "- **风速**：3米/秒\n",
      "- **气压**：1015百帕\n",
      "\n",
      "**穿衣建议：**\n",
      "- 白天建议穿着长袖上衣和轻薄的外套，早晚气温较低，建议添加保暖衣物。\n",
      "\n",
      "**健康提示：**\n",
      "- 空气质量良好，适宜户外运动，但早晚温差较大，注意保暖，预防感冒。\n",
      "\n",
      "**出行建议：**\n",
      "- 天气晴朗，视线良好，适宜驾车和骑行。下午局部多云，注意行车安全。\n",
      "\n",
      "今日北京的气温适中，天气状况良好，是进行户外活动和旅游的好时机。市民在享受秋日阳光的同时，也应注意早晚温差，做好保暖措施。\n"
     ]
    }
   ],
   "source": [
    "print(llm_engine([{'role':'user','content':hyde_prompt.format_map({'query':'今天北京天气多少度',\n",
    "                                                                          'chunk_size':512})}]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7e1cd4b-49ce-485a-9b54-3f8b075327cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0rc2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
